package com.heying.mybatis;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heying.mybatis.entity.Student;
import com.heying.mybatis.entity.Teacher;
import com.heying.mybatis.mapper.StudentMapper;
import com.heying.mybatis.mapper.TeacherMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * 根据老师姓名和年龄动态条件分页查询老师及其关联的学生信息，要求用表连接查询
 */

public class Test {

    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().
                build(inputStream);

        SqlSession session = sqlSessionFactory.openSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);
//        List<Student> studentList = mapper.queryAll();
//        System.out.println(studentList);
//
//        Student student = mapper.queryById(1);
//        System.out.println(student);

        Student student = (new Student(1, "s6", "vvv", new Date(),
                1, 1, "sport"));
//        mapper.insertOne(student);
//        System.out.println(student);
//        mapper.updateOne(student);
//        mapper.delOne(1);

//        Student student1 = mapper.queryOneAndTeacherN1(2);
//        System.out.println(student1);
//        System.out.println(student1.getTeacher());

//        List<Student> studentList = mapper.queryAllAndTeacherN1();
//        studentList.forEach(Student::getTeacher);

//        session.commit();

        TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class);
//        List<Teacher>allAndStudents = teacherMapper.findAllAndStudents();
//        System.out.println(mapper.queryAllAndTeacher());
//
//        Student update = new Student();
//        update.setId(3);
//        update.setStuno("303");
//        update.setName("jjj");
//        mapper.updateIfNecessary(update);
//        mapper.deleteSome(new Integer[]{4,5});

        Student condition = new Student();
        condition.setName("s");
//        condition.setSex(0);
        Teacher tcon = new Teacher();
        tcon.setName("t2");
        condition.setTeacher(tcon);
        //分页
        Page<Student>page = PageHelper.startPage(1,3);
        List<Student>studentList = mapper.queryAllAndTeacher();
//        List<Student>studentList2 = mapper.querySomeAndTeacherByCondition(condition);
        System.out.println("students="+studentList);
        System.out.println("total="+page.getTotal());

//        session.commit();
    }
}
